<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>高维数组转化</title>
</head>

<body>
  <script>
    // 转换数组
    /*
      将[1,2,[3,[4,5],6],7,[8],9]装换为
      {
        children:[
          {value:1},
          {value:2},
          children:[
          {value:3},
          children:[
          {value:4},
          ...
          ]
          ]
        ]
      }
    */

    var arr = [1, 2, [3, [4, 5], 6], 7, [8], 9]

    //  写法1：转换数组
    function cover(arr) {
      var result = []
      for (let i = 0; i < arr.length; i++) {
        if(typeof arr[i] == 'number'){
          result.push({
            value:arr[i]
          })
        }else if(Array.isArray(arr[i])){
          result.push({
            children:cover(arr[i])
          })
        }
      }
      return result
    }
    console.log(cover(arr))

    // 写法2
    function conver2(item){
      if(typeof item == 'number'){
        return {
          value:item
        }
      }else if(Array.isArray(item)){
        return {
          children:item.map(item=>conver2(item))
        }
      }
    }
    console.log(conver2(arr))
  </script>
</body>

</html>